<%@page import="java.sql.ResultSet"%>
<%
if (request.getProtocol().compareTo("HTTP/1.0")==0)
      response.setHeader("Pragma","no-cache");
if (request.getProtocol().compareTo("HTTP/1.1")==0)
      response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
    <title>Searched Course -- Student Registration System</title>

</head>
<body style="text-align: center" bgcolor="#ffff00">
<div class="main">
  <div class="header">
    <div class="resize">
      <div class="logo"><img src="images/logo.png" alt="picture" width="171" height="42" border="0" /></a></div>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  <div class="slider_resize">
    <div class="clr"></div>
  </div>
  <div class="FBG">
    <div class="clr"></div>
  </div>
  <div class="body">
   <h1>Search Results</h1>


<%                    
String itemName = request.getParameter("itemName");
String itemType = request.getParameter("itemType");
String itemYear = request.getParameter("itemYear");
String openAuctionsOnly = request.getParameter("openAuctionsOnly");

String strId;

String mysJDBCDriver = (String)session.getValue("mysJDBCDriver"); 
String mysURL = (String)session.getValue("mysURL"); 
String mysUserID = (String)session.getValue("mysUserID");
String mysPassword = (String)session.getValue("mysPassword");
        
String username = ""+session.getValue("login");
%>
	<table border="1" align="center">
		<tr>
			<td>Auction Number</td>
			<td>Item Name</td>
			<td>Item Year</td>
			<td>Item Type</td>
			<td>Seller</td>
			<td>Close Date</td>
		</tr>
<%
	java.sql.Connection conn=null;
	try 
	{
       	Class.forName(mysJDBCDriver).newInstance();
		java.util.Properties sysprops=System.getProperties();
		sysprops.put("user",mysUserID);
		sysprops.put("password",mysPassword);
      
		//connect to the database
		conn=java.sql.DriverManager.getConnection(mysURL,sysprops);
		System.out.println("Connected successfully to database using JConnect");

		java.sql.Statement stmt=conn.createStatement();
        java.sql.Statement stmt1=conn.createStatement();
		java.sql.Statement stmt2=conn.createStatement();
		java.sql.ResultSet finalResult,rs1,rs2;  
		finalResult = null;
		
		String selectFromWhere = "select Auction.AuctionId, Auction.CloseDate, Item.Name, Item.YearManufactured, Item.Type, Selling.UserName from Auction, Item, Selling where Auction.AuctionId = Selling.AuctionId and Item.ItemId = Selling.ItemId";
		
		String openAuctionCondition = new String(); 
		String itemTypeCondition = new String();
		String itemYearCondition = new String(); 
		String itemNameCondition = new String();
		
		if(openAuctionsOnly != null && openAuctionsOnly.equals("on"))
		{
			openAuctionCondition = " and Auction.CloseDate > now() ";
		}
		if (itemName != null && !itemName.equals(""))
		{
			itemNameCondition = " and Item.Name like '%" + itemName + "%' ";
		}
		if (itemYear != null && !itemYear.equals(""))
		{
			itemYearCondition = " and Item.YearManufactured = " + itemYear + " ";
		}
		if (itemType != null && !itemType.equals(""))
		{
			itemTypeCondition = " and Item.Type = '" + itemType + "' ";
		}
		
		finalResult = stmt.executeQuery(selectFromWhere + itemNameCondition + itemTypeCondition + itemYearCondition + openAuctionCondition);
		while (finalResult.next())
		{
						%>
					 <tr>
					 	<td><a href="#" onclick="window.open('AuctionInfo.jsp?auctionId=<%=finalResult.getInt(1)%>','_self')";><%=finalResult.getString(1) %></td>
					 	<td><%=finalResult.getString(3) %></td>
					 	<td><%=finalResult.getString(4) %></td>
					 	<td><%=finalResult.getString(5) %></td>
					 	<td><%=finalResult.getString(6) %></td>
					 	<td><%=finalResult.getString(2) %></td>
					 </tr>
					 <%		 		   			
		}		   	
 	} catch(Exception e)
	{
		e.printStackTrace();
		out.print(e.toString());
	}
	finally{
	
		try{conn.close();}catch(Exception ee){};
	}
	
	
%>

	</table>
	</br>
<% 
String status = (String)session.getValue("status");
if (status.equals("manager"))
{
	   %><input type="button" value="Home" onclick="window.open('ManagerHomePage.jsp','_self');" /><%
}
else if (status.equals("employee"))
{
	  %><input type="button" value="Home" onclick="window.open('EmployeeHomePage.jsp','_self');" /><%
}
else
{
	  %><input type="button" value="Home" onclick="window.open('CustomerHomePage.jsp','_self');" /><%
}

%><input type="button" value="Logout" onclick="window.open('index.htm','_self');" />
  </div>
	  <div class="footer">
    <div class="resize" style="background:#141414;">
				<p class="leftt">
					Created by: Andrew Gratta, Sarah Jordan, and Kevin O'Flaherty<br /></p>
      <p class="rightt"><span class="leftt">Copyright. All Rights Reserved</span></p>
      <p class="rightt">Developed for CSE305 at Stony Brook University</p>
      <div class="clr"></div>
    </div>
  </div>
</div>
</body>
</html>
